﻿#include "ACT/LogSaver.h"

#include <qdebug.h>

#include "MacroDefinition.h"

#ifndef CONFIGFILE_ON//如果没有配置文件
#define LOGSAVER_ON//开启日志
#ifdef LOGSAVER_ON
#define LOGSAVER_CONSOLE_PRINT//开启控制台打印
#define LOGSAVER_FILE_PRINT//开启文件打印
//#define LOGSAVER_DEBUG//开启debug日志
#define LOGSAVER_INFORMATION//开启Information日志
#define LOGSAVER_WARNING//开启warning日志
#define LOGSAVER_CRITICAL//开启critical日志
#endif
#endif


LogSaver* LogSaver::logSaver=Q_NULLPTR;
LogSaver::LogSaver(QObject* parent) :QObject(parent)
{
  logSaver=this;
  setObjectName("logSaver");
#ifdef LOGSAVER_DEBUG
  enableLogLevel(Debug);
#endif
#ifdef LOGSAVER_INFORMATION
  enableLogLevel(Information);
#endif
#ifdef LOGSAVER_WARNING
  enableLogLevel(Warning);
#endif
#ifdef LOGSAVER_CRITICAL
  enableLogLevel(Critical);
#endif
#ifdef LOGSAVER_FILE_PRINT
  int i=1;
  while (true)
    {
      if(QFile::exists(logFilePath+logFileSuffix))
        {
          logFilePath=logFilePath.remove(logFilePath.size()-1,1)+QString::number(i++);
        }
      else
        {
          break;
        }
    }
  qfile=new QFile(logFilePath+logFileSuffix);
  qfile->open(QFile::Append);
#endif
  LogSaver1* logSaver1=new LogSaver1();
  QThread* qThread=new QThread();
  logSaver1->moveToThread(qThread);
  connect(this,&LogSaver::signal_writeLogToFile,logSaver1,&LogSaver1::slot_writeLogToFile,Qt::QueuedConnection);
  connect(this,&LogSaver::destroyed,qThread,&QThread::quit,Qt::QueuedConnection);
  connect(qThread,&QThread::finished,logSaver1,&QObject::deleteLater,Qt::QueuedConnection);
  connect(qThread,&QThread::finished,qThread,&QObject::deleteLater,Qt::QueuedConnection);
  qThread->start();
}

LogSaver::~LogSaver()
{
  qfile->close();
  delete qfile;
}

LogSaver* LogSaver::getLogSaver()
{
  return Q_NULLPTR==logSaver?logSaver=new LogSaver():logSaver;
}

void LogSaver::saveLog(QString msg, uchar logFlag)
{
  if (logLevel & logFlag)
    {
      QString log = QDateTime::currentDateTime().toString(QStringLiteral("yyyy.MM.dd hh:mm:ss"))+getQStringOfLogFlag(logFlag) + msg;
#ifdef LOGSAVER_CONSOLE_PRINT
      qDebug()<<log;
#endif
#ifdef LOGSAVER_FILE_PRINT
      emit signal_writeLogToFile(log+newLine,qfile);
#endif
    }
}

void LogSaver::enableLogLevel(uchar logFlag)
{
  logLevel |= logFlag;
}

void LogSaver::disableLogLevel(uchar logFlag)
{
  logLevel &= (~logFlag);
}
QString LogSaver::getQStringOfLogFlag(uchar logFlag)
{
  switch (logFlag)
    {
    case LogSaver::Debug:
      return QString_debug;
    case LogSaver::Information:
      return QString_information;
    case LogSaver::Warning:
      return QString_warning;
    case LogSaver::Critical:
      return QString_critical;
    default:
      return "";
    }
}

int LogSaver::getLogLevel()
{
  return logLevel;
}

QString LogSaver::getLogFilePath()
{
  return logFilePath;
}

void LogSaver::setLogFilePath(QString filePath)
{
  this->logFilePath = filePath;
}


LogSaver1::LogSaver1()
{
}

LogSaver1::~LogSaver1()
{
}

void LogSaver1::slot_writeLogToFile(QString msg,QFile* file)
{
  QByteArray data=msg.toLocal8Bit();
  file->write(data);
  file->flush();
}


